package com.zztl.infiniteBand.presenter

import android.content.Context
import android.text.TextUtils
import com.zztl.infiniteBand.R
import com.zztl.infiniteBand.base.BasePresenter
import com.zztl.infiniteBand.model.NoDataResponseBean
import com.zztl.infiniteBand.model.UploadFileBean
import com.zztl.infiniteBand.net.FilterSubscriber
import com.zztl.infiniteBand.net.RetorfitServiceManger
import com.zztl.infiniteBand.net.converter.DataConverter
import com.zztl.infiniteBand.net.imp.RequestKeys
import com.zztl.infiniteBand.net.requestBean.CertificateRequest
import com.zztl.infiniteBand.ui.view.mvpview.CertificateView
import com.zztl.infiniteBand.util.UiUtils
import com.zztl.infiniteBand.util.toast
import java.io.File

/**
 * @desc
 * @auth ${user}
 * @time 2018/6/14  14:18
 */
class CertificatePresenter : BasePresenter<CertificateView>() {
    fun check(name: String, cardNum: String, frontPath: String?, reversePath: String?, holdPath: String?): Boolean {
        if (TextUtils.isEmpty(name)) {
            UiUtils.context.toast(UiUtils.getString(R.string.姓名不能为空))
            return false
        }
        if (TextUtils.isEmpty(cardNum)) {
            UiUtils.context.toast(UiUtils.getString(R.string.证件号不能为空))
            return false
        }
        if (TextUtils.isEmpty(frontPath)) {
            UiUtils.context.toast(UiUtils.getString(R.string.请上传证件正面照))
            return false
        }
        if (TextUtils.isEmpty(reversePath)) {
            UiUtils.context.toast(UiUtils.getString(R.string.请上传证件反面照))
            return false
        }
        if (TextUtils.isEmpty(holdPath)) {
            UiUtils.context.toast(UiUtils.getString(R.string.请上传手持证件照))
            return false
        }
        return true
    }

    fun certificate(request: CertificateRequest, context: Context) {
        addSubscribe(RetorfitServiceManger.observe(RetorfitServiceManger.instance!!.apiService.certificate(DataConverter.instance.toJson(request)))
                .subscribe(object : FilterSubscriber<NoDataResponseBean>(context) {
                    override fun success(bean: NoDataResponseBean) {
                        view!!.submitSuccess()
                    }


                }))
    }

    fun uploadPhoto(file: File?, type: String, context: Context) {
        if (null != file) {
//            val path = file.absolutePath
            addSubscribe(RetorfitServiceManger
                    .observe(RetorfitServiceManger.instance!!.apiService
                            .uploadImage(RetorfitServiceManger.parseImageRequestBody(RequestKeys.PHOTO, file),
                                    RetorfitServiceManger.parseRequestBody(type)))
                    .subscribe(object : FilterSubscriber<UploadFileBean>(context) {
                        override fun success(bean: UploadFileBean) {
                            view!!.loadSuccess(bean)
                        }

                        override fun fail() {
                            super.fail()
                            view!!.loadFail()
                        }
                    }))

        }
    }


}
